/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package utils;

import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;

import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 发送电子邮件
 */
public class SendMailUtil {

    private static final String from = "jasonli@kuainiugroup.com";
    private static final String fromName = "李宁";
    private static final String charSet = "utf-8";
    private static final String username = "jasonli@kuainiugroup.com";
    private static final String password = "VidWkugxQa97dWTA";

    private static Map<String, String> hostMap = new HashMap<String, String>();

    static {
        hostMap.put("smtp.126", "smtp.126.com");// 126
        hostMap.put("smtp.qq", "smtp.qq.com");// qq
        hostMap.put("smtp.163", "smtp.163.com");// 163
        hostMap.put("smtp.sina", "smtp.sina.com.cn");// sina
        hostMap.put("smtp.tom", "smtp.tom.com");// tom
        hostMap.put("smtp.263", "smtp.263.net");// 263
        hostMap.put("smtp.yahoo", "smtp.mail.yahoo.com");// yahoo
        hostMap.put("smtp.hotmail", "smtp.live.com");// hotmail
        hostMap.put("smtp.gmail", "smtp.gmail.com");// gmail
        hostMap.put("smtp.port.gmail", "465");
        hostMap.put("smtp.weidu", "smtp.exmail.qq.com");//weidu.ac.cn
        hostMap.put("smtp.port.weidu", "465");
        hostMap.put("smtp.kuainiugroup", "smtp.exmail.qq.com");//kuainiugroup.com
        hostMap.put("smtp.port.kuainiugroup", "465");
    }

    public static String getHost(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp." + matcher.group(1);
        }
        if (hostMap.containsKey(key)) {
            return hostMap.get(key);
        } else {
            throw new Exception("unSupportEmail");
        }
    }

    public static int getSmtpPort(String email) throws Exception {
        Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");
        Matcher matcher = pattern.matcher(email);
        String key = "unSupportEmail";
        if (matcher.find()) {
            key = "smtp.port." + matcher.group(1);
        }
        if (hostMap.containsKey(key)) {
            return Integer.parseInt(hostMap.get(key));
        } else {
            return 25;
        }
    }


    /**
     * 发送普通邮件
     *
     * @param toMailAddr 收信人地址
     * @param subject    email主题
     * @param message    发送email信息
     */
    public static void sendCommonMail(String toMailAddr, String subject, String message) {
        HtmlEmail hemail = new HtmlEmail();
        try {
            hemail.setHostName(getHost(from));
            hemail.setSmtpPort(getSmtpPort(from));
            hemail.setSSLOnConnect(true);
            hemail.setCharset(charSet);
            //hemail.addTo("barrychen@kuainiugroup.com", "陈鹏");
            hemail.addTo("jasonli@kuainiugroup.com", "李宁");
            hemail.setFrom(from, fromName);
            hemail.setAuthentication(username, password);
            hemail.setSubject(String.format("%s(%s)", "通话日报", DateUtils.formatDate("yyyy-MM-dd")));
            hemail.setHtmlMsg("<a href=\"/blog/1559893\">Spring Multipart email 的基本使用</a>");
            hemail.setMsg(message);
            hemail.send();
            System.out.println("email send true!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("email send error!");
        }
    }

    public static void sendReport(String filePath) {
        MultiPartEmail multiPartEmail = new MultiPartEmail();
        try {
            String reportDate = DateUtils.formatDate("yyyy-MM-dd",DateUtils.getBeforeTime(1));

            multiPartEmail.setHostName(getHost(from));
            multiPartEmail.setSmtpPort(getSmtpPort(from));
            multiPartEmail.setSSLOnConnect(true);
            multiPartEmail.setCharset(charSet);
            multiPartEmail.addTo("barrychen@kuainiugroup.com", "陈鹏");
            //multiPartEmail.addTo("dingmingliang@kuainiugroup.com", "丁明亮");
            multiPartEmail.addTo("jasonli@kuainiugroup.com", "李宁");
            multiPartEmail.setFrom(from, fromName);
            multiPartEmail.setAuthentication(username, password);
            multiPartEmail.setSubject(String.format("%s(%s)", "通话日报", reportDate));
            multiPartEmail.setMsg("Dear All: \n\t附件为"+reportDate+"通话日报,请查收。\n\t有问题请及时联系:jasonli@kuainiujinkegroup.com");
            //multiPartEmail.attach(new File(filePath));
            multiPartEmail.send();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        SendMailUtil.sendCommonMail("jasonli@kuainiugroup.com", "斗牙戈", "send success!");
        //SendMailUtil.sendReport("/Users/lining/Desktop/GenesysReport/20180929162416915.xls");
    }

}